package lucene;

import static org.junit.Assert.*;

import java.io.File;
import java.io.IOException;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class fileManagerTest {
	
	IndexingProxy testedClass;
	File testedFile = new File("D:/XMouseButtonControl.log");
	File testedDirectory = new File("D:/");
	File testedHugeFile = new File("D:/Torrentz/NFSU2/CD1/rld-nu2a.txt");
	File testedNonExistentFile = new File("D:/XMBCSettings.xml");
	

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
		
		testedClass = new fileManager();
	}

	@After
	public void tearDown() throws Exception {
	}
	
	@Test
	public void testAddFileToRemoteDirectory() 
	{
		try 
		{
			testedClass.addFile(testedFile, testedFile.getAbsolutePath(), "C:/");
		} 
		catch (IOException e) 
		{
			fail("File add test launched unhandled exception, failed, why?");
		}
	}
	
	@Test
	public void testAddFileToPreviousLocation()
	{
		try 
		{
			testedClass.addFile(testedFile, testedFile.getAbsolutePath(), testedFile.getAbsolutePath());
		} 
		catch (IOException e) 
		{
			fail("File add test launched exception, failed, cannot add log file to same place from whence the file came");
		}
	}
	
	@Test
	public void testAddFileOneHundredTimes()
	{
		try 
		{
			for(int i=0;i<=100;i++)
			{
				testedClass.addFile(testedFile, testedFile.getAbsolutePath(), "C:/");
			}
		} 
		catch (IOException e) 
		{
			fail("Exception Launched, why?");
		}
	}
	
	/*
	@Test
	public void testAddHugeFile()
	{
		try 
		{
			testedClass.addFile(testedHugeFile, testedHugeFile.getAbsolutePath(), "C:/");
		} 
		catch (IOException e) 
		{
			fail("File too large launched an unhandled exception");
		}
	}
	*/
	
	@Test
	public void testAddDirectory()
	{
		try 
		{
			testedClass.addFile(testedDirectory, "D:/", testedFile.getAbsolutePath());
		} 
		catch (IOException e) 
		{
			fail("File add test launched exception, failed, cannot add log file from directory");
		}
		
	}
	
	
	@Test
	public void testRemoveExistingFile() 
	{
		try
		{
			testedClass.removeFile(testedFile);
		}
		catch(Exception e)
		{
			fail("Should not launch unhandled exception.");
		}	
	}
	
	@Test
	public void testRemoveNonExistingFile() 
	{
		try
		{
			testedClass.removeFile(testedNonExistentFile);
		}
		catch(Exception e)
		{
			fail("Y U Fail?");
		}
		
		fail("Should not work!");
	}
	
	@Test
	public void testRemoveNullFile() 
	{
		try
		{
			testedClass.removeFile(null);
		}
		catch(Exception e)
		{
			fail("Launched unhandled exception, should be handled");
		}	
	}
	
	@Test
	public void testRemoveDirectory() 
	{
		try
		{
			testedClass.removeFile(new File("D:/"));
		}
		catch(Exception e)
		{
			fail("Test failed?");
		}	
	}

	@Test
	public void testSelectNullDisplayVersion() 
	{
		try
		{
			testedClass.selectDisplayVersion(null);
		}
		catch(Exception e)
		{
			fail("Null display version launches unhandled exception, revise!");
		}
	}
	
	@Test
	public void testSelectZeroDisplayVersion() 
	{
		try
		{
			testedClass.selectDisplayVersion("1");
		}
		catch(Exception e)
		{
			e.printStackTrace();
			fail("Zero display version launches unhandled exception, revise!");
		}
	}

	@Test
	public void testSelectCurrentFile() 
	{
		try 
		{
			testedClass.selectCurrentFile(testedFile);
		} catch (Exception e) 
		{
			fail("Launches unhandled exception, revise!");
		}
	}
}
